﻿package mortal.game.control
{
    import com.gengine.debug.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.mail.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MailController extends Controller
    {
        private var _mailCache:MailCache;
        private var _mailProxy:MailProxy;
        private var _mailModule:MailModule;

        public function MailController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._mailCache = cache.mail;
            this._mailProxy = GameProxy.mail;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._mailModule == null)
            {
                this._mailModule = new MailModule();
                this._mailModule.addEventListener(WindowEvent.SHOW, this.onMailShow);
                this._mailModule.addEventListener(WindowEvent.CLOSE, this.onMailClose);
            }
            return this._mailModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.MailMenuSend, this.clickMenuSendMail);
            Dispatcher.addEventListener(EventName.MailSend, this.sendMailHandler);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.levelUpgrade);
            return;
        }// end function

        protected function onMailShow(event:Event) : void
        {
            cache.mail.mailNotice = false;
            NetDispatcher.dispatchCmd(ServerCommand.MailNotice, null);
            NetDispatcher.addCmdListener(ServerCommand.MailListUpdate, this.mailListUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.MailUpdate, this.mailUpdateHandler);
            Dispatcher.addEventListener(EventName.MailQueryAll, this.queryAllMailHandler);
            Dispatcher.addEventListener(EventName.MailQueryUnread, this.queryUnReadMailHandler);
            Dispatcher.addEventListener(EventName.MailQueryRead, this.queryReadMailHandler);
            Dispatcher.addEventListener(EventName.MailQuerySys, this.querySysMailHandler);
            Dispatcher.addEventListener(EventName.MailQueryPers, this.queryPersMailHandler);
            Dispatcher.addEventListener(EventName.MailRead, this.readMailHandler);
            Dispatcher.addEventListener(EventName.MailDelete, this.deleteMailHandler);
            Dispatcher.addEventListener(EventName.MailGetAttachment, this.getAttachmentHandler);
            Dispatcher.addEventListener(EventName.MailGetAttachments, this.getAttachmentsHandler);
            Dispatcher.addEventListener(EventName.MailGASucced, this.getAttachmentsSuccedHandler);
            Dispatcher.addEventListener(EventName.MailDelteSucced, this.delteSuccedHandler);
            Dispatcher.addEventListener(EventName.MailSendSucced, this.mailSendSuccedHandler);
            if ((view as MailModule).uiLoadComplete)
            {
                this.queryAllMailHandler(null);
            }
            return;
        }// end function

        protected function onMailClose(event:Event) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MailListUpdate, this.mailListUpdateHandler);
            NetDispatcher.removeCmdListener(ServerCommand.MailUpdate, this.mailUpdateHandler);
            Dispatcher.removeEventListener(EventName.MailQueryAll, this.queryAllMailHandler);
            Dispatcher.removeEventListener(EventName.MailQueryUnread, this.queryUnReadMailHandler);
            Dispatcher.removeEventListener(EventName.MailQueryRead, this.queryReadMailHandler);
            Dispatcher.removeEventListener(EventName.MailQuerySys, this.querySysMailHandler);
            Dispatcher.removeEventListener(EventName.MailQueryPers, this.queryPersMailHandler);
            Dispatcher.removeEventListener(EventName.MailRead, this.readMailHandler);
            Dispatcher.removeEventListener(EventName.MailDelete, this.deleteMailHandler);
            Dispatcher.removeEventListener(EventName.MailGetAttachment, this.getAttachmentHandler);
            Dispatcher.removeEventListener(EventName.MailGetAttachments, this.getAttachmentsHandler);
            Dispatcher.removeEventListener(EventName.MailGASucced, this.getAttachmentsSuccedHandler);
            Dispatcher.removeEventListener(EventName.MailDelteSucced, this.delteSuccedHandler);
            Dispatcher.removeEventListener(EventName.MailSendSucced, this.mailSendSuccedHandler);
            return;
        }// end function

        private function levelUpgrade(event:Event) : void
        {
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            if (_loc_2 >= 15 && Cache.instance.mail.mailNotice)
            {
                NetDispatcher.dispatchCmd(ServerCommand.MailNotice, null);
            }
            return;
        }// end function

        private function queryUnReadMailHandler(event:DataEvent) : void
        {
            this._mailProxy.queryMail(2, 0, 1, int(event.data));
            return;
        }// end function

        private function queryReadMailHandler(event:DataEvent) : void
        {
            this._mailProxy.queryMail(2, 0, 2, int(event.data));
            return;
        }// end function

        private function querySysMailHandler(event:DataEvent) : void
        {
            this._mailProxy.queryMail(1, 1, 0, int(event.data));
            return;
        }// end function

        private function queryPersMailHandler(event:DataEvent) : void
        {
            this._mailProxy.queryMail(1, 2, 0, int(event.data));
            return;
        }// end function

        private function queryAllMailHandler(event:DataEvent) : void
        {
            if (event && event.data)
            {
                this._mailProxy.queryMail(0, 0, 0, int(event.data));
            }
            else
            {
                this._mailProxy.queryMail(0, 0, 0, 0);
            }
            return;
        }// end function

        private function getAttachmentHandler(event:DataEvent) : void
        {
            if (event.data)
            {
                this._mailProxy.getMailAttachment(event.data.mailId, event.data.attachementIndex, event.data.isDelete);
            }
            return;
        }// end function

        private function deleteMailHandler(event:DataEvent) : void
        {
            if (event.data)
            {
                this._mailProxy.deleteMail(event.data.arr as Array, event.data.boo as Boolean);
            }
            return;
        }// end function

        private function getAttachmentsHandler(event:DataEvent) : void
        {
            if (event.data)
            {
                this._mailProxy.getMailAttachments_async(event.data as Array);
            }
            return;
        }// end function

        private function readMailHandler(event:DataEvent) : void
        {
            if (event.data)
            {
                this._mailProxy.readMail(event.data as Number);
            }
            return;
        }// end function

        private function sendMailHandler(event:DataEvent) : void
        {
            if (event.data)
            {
                this._mailProxy.sendMail(event.data.toPlayerName, event.data.title, event.data.content);
            }
            return;
        }// end function

        private function mailSendSuccedHandler(param1:Object) : void
        {
            (view as MailModule).mailWritePanel.clearWord();
            return;
        }// end function

        private function getAttachmentsSuccedHandler(event:DataEvent) : void
        {
            (view as MailModule).queryMail();
            return;
        }// end function

        private function delteSuccedHandler(event:DataEvent) : void
        {
            (view as MailModule).clearMailSelect();
            (view as MailModule).mailReadPanel.checkMail();
            (view as MailModule).queryMail();
            return;
        }// end function

        private function mailListUpdateHandler(param1:Object) : void
        {
            Log.debug("mailList刷新");
            (view as MailModule).updateMailList();
            return;
        }// end function

        private function mailUpdateHandler(param1:Object) : void
        {
            Log.debug("mail刷新");
            (view as MailModule).updateMailList(true);
            return;
        }// end function

        private function clickMenuSendMail(event:DataEvent) : void
        {
            (view as MailModule).openWritePanelFunc(event.data as String);
            GameManager.instance.popupView(this.view);
            return;
        }// end function

    }
}
